package com.nefu.softlab.xjwc.controller;

import com.nefu.softlab.xjwc.model.Document;
import com.nefu.softlab.xjwc.service.DocumentService;
import com.nefu.softlab.xjwc.utils.JSONResult;
import com.nefu.softlab.xjwc.utils.JsonUtil;
import com.nefu.softlab.xjwc.utils.LoggerUtil;
import com.nefu.softlab.xjwc.utils.XJWCException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author LiXiwen
 * @date 2020/2/8 11:23
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
public class DocumentApi {
    private final LoggerUtil logger;
    private final DocumentService documentService;

    @Autowired
    public DocumentApi(LoggerUtil logger, DocumentService documentService) {
        this.logger = logger;
        this.documentService = documentService;
    }

    @PostMapping("/document/upload")
    public JSONResult uploadDocument(@RequestPart(value = "file", required = false) MultipartFile[] files, @RequestPart(value = "document") Document document) throws XJWCException {
        if (null == document || StringUtils.isEmpty(document.getProjectId())) {
            return JSONResult.errorMessage("项目id不能为空!");
        }
        logger.info("POST uploadDocument : document : " + JsonUtil.getJsonString(document));
        return documentService.uploadDocument(files, document);
    }

    @PutMapping("/document/update")
    public JSONResult updateDocument(@RequestPart(value = "file", required = false) MultipartFile[] files, @RequestPart(value = "document") Document document) throws XJWCException {
        if (null == document || StringUtils.isEmpty(document.getProjectId())) {
            return JSONResult.errorMessage("文件内容不能为空!");
        }

        System.out.println("file : " + files.length);
        logger.info("PUT updateDocument : document : " + JsonUtil.getJsonString(document));
        return JSONResult.ok(documentService.updateDocument(files, document));
    }


    @GetMapping("/document/preview/{projectId}")
    public JSONResult previewFiles(@PathVariable("projectId") Integer projectId) {
        if (StringUtils.isEmpty(projectId)) {
            return JSONResult.errorMessage("项目id不能为空!");
        }
        return JSONResult.ok(documentService.previewFiles(projectId));
    }

    @GetMapping("/document/search/{projectId}")
    public JSONResult selectDocument(@PathVariable Integer projectId) throws XJWCException {
        return documentService.getFileName(projectId);
    }

    @GetMapping("/document/download/{projectId}/{name}")
    public String downloadDocument(@PathVariable Integer projectId, @PathVariable String name, HttpServletResponse response, HttpServletRequest request) throws XJWCException {
        if (null == projectId || 0 >= projectId) {
            return "参数有误，请重新点击下载!";
        }
        logger.info("GET downloadDocument : projectId = " + projectId + " , fileName = " + name);
        return documentService.downloadDocument(projectId, name, response, request);
    }

    /*@RequiresRoles("admin")
    @DeleteMapping("/document/del/{projectId}/{name}")
    public JSONResult deleteDocument(@PathVariable Integer projectId, @PathVariable String name) throws XJWCException {
        if (null == projectId || 0 >= projectId) {
            return JSONResult.errorMessage("参数有误，请重新点击删除!");
        }
        logger.info("DELETE deleteDocument : projectId = " + projectId + " , fileName = " + name);
        return documentService.deleteDocument(projectId, name);
    }*/

    @DeleteMapping("/document/del")
    public JSONResult deleteDocument(@RequestParam("projectId") Integer projectId, @RequestParam("name") String name) throws XJWCException {
        if (null == projectId || 0 >= projectId) {
            return JSONResult.errorMessage("参数有误，请重新点击删除!");
        }
        logger.info("DELETE deleteDocument : projectId = " + projectId + " , fileName = " + name);
        return documentService.deleteDocument(projectId, name);
    }
}
